import React, { createContext, FC } from 'react';
// import { message } from 'antd';

interface BaseContextProps {
  devType: string;
  title: string;
  tabName: string;
  tabKey: string;
  group: any;
  isReload: boolean;
  setIsReload: (v: boolean) => void;
  groupActionDrawer: string;
  showMessage: (type: 'success' | 'error' | 'info' | 'warning', content: string) => void;
  contextHolder: any;
  wifiTabKey?: string;
  formRef?: any;
}

type AllContextType = BaseContextProps & { [key: string]: any };

const AllContext = createContext<AllContextType>({} as AllContextType);

interface AllContextProviderProps {
  value: any;
  children: React.ReactNode;
}

const AllContextProvider: FC<AllContextProviderProps> = ({ value, children }) => {
  return (
    <AllContext.Provider value={{
      ...value,
    }}>
      {/* {contextHolder} */}
      {children}
    </AllContext.Provider>
  );
};

// 创建通用Provider 实例
const CreateProviderContextInstance = (baseContext: any, data: any, Component: React.ComponentType<any>) => {
  const newAllContext = { ...baseContext };
  // 改造为 data 的 key-value 形式
  Object.keys(data).forEach(key => {
    newAllContext[key] = data[key];
  });

  return (
    <AllContextProvider value={newAllContext}>
      <Component />
    </AllContextProvider>
  );
};

export { AllContext, AllContextProvider, CreateProviderContextInstance};